/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

/**
 *
 * @author CALDERON
 */
import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.HibernateUtilZoologia;
import muhnes.sizoo.modelo.Acuatico;

public class AcuaticoDaoImpl implements AcuaticoDao {
     Session sesion;
    
    public AcuaticoDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Acuatico acuatico ) {
        Transaction transaccion = sesion.beginTransaction();
       sesion.save( acuatico );
        transaccion.commit();
    }

        
    @Override
    public void guardarColeccion( List< Acuatico > acuaticos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( acuaticos );
        transaccion.commit();
    }

    @Override
    public void actualizar( Acuatico acuatico ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( acuatico );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Acuatico > acuaticos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( acuaticos );
        transaccion.commit();
    }

    @Override
    public void eliminar( Acuatico acuatico ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( acuatico );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Acuatico > acuaticos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( acuaticos );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Acuatico acuatico = buscarPorId( id );
        sesion.delete( acuatico );
        transaccion.commit();
    }

    @Override
    public Acuatico buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Acuatico where codigoacuatico = " + id);
        Acuatico acuatico = (Acuatico)consulta.uniqueResult();
        transaccion.commit();
        return acuatico;
    }

    @Override
    public List< Acuatico > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Acuatico");
        List< Acuatico > acuaticos = consulta.list();
        transaccion.commit();
        return acuaticos;
    } 
}
